package com.book.settings.service.impl;

import com.book.common.UUidUtil;
import com.book.settings.mapper.AdminMapper;
import com.book.settings.pojo.Admin;
import com.book.settings.pojo.User;
import com.book.settings.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.Property;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

import static com.book.common.load.loadPropByInputStream;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin searchAdminById(String id) {
        Admin admin = adminMapper.searchAdminById(id);
        return admin;
    }

    @Override
    public Integer addAdmin(Admin admin) throws IOException {
        Admin admin1 = adminMapper.getAdminByAct(admin.getAct());
        if(admin1==null){
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowDate = dateFormat.format(date);
            admin.setRegdate(nowDate);
            admin.setId(UUidUtil.getUUID());
            // InputStream ips = new FileInputStream(new File("D:\\spring练习\\untitled1\\untitled\\src\\main\\resources\\license.properties"));
            // InputStream res = ClassLoader.getSystemClassLoader().getResourceAsStream("license.properties");
            // Properties prop = new Properties();
            // prop.load(ips);
            // String value = prop.getProperty("license");
            ResourceBundle bundle = ResourceBundle.getBundle("license");
            String value = bundle.getString("license");
            System.out.println(value);
            if(value.equals(admin.getLicense())){
                return adminMapper.addAdmin(admin);
            }else{
                return -2;
            }

        }else{
            return -1;
        }
    }

    @Override
    public Integer updateAdminById(Admin admin) {
        int updateAdminResult = adminMapper.updateAdminById(admin);
        return updateAdminResult;
    }
}
